myApp.directive("uTestRecord", function() { 
  return { 
    restrict: "E",        // 指令是一个元素 (并非属性) 
    scope: {              // 设置指令对于的scope 
      // name: "@",          // name 值传递 （字符串，单向绑定） 
      data: "=",        // amount 引用传递（双向绑定） 
      projectId:"@",        // 保存操作 
      isProject:"="
    }, 
    templateUrl:"/beetle/App/module/template/tpl/uTestRecord.html",
    replace: true,        // 使用模板替换原始标记 
    transclude: false,    // 不复制原始HTML内容 
    controller: [ "$scope","$http","$filter", function ($scope,$http,$filter) { 
       
    	$scope.info={};
        $scope.info.status="ALL";
        $scope.info.url=beetle_plan_url+"/autotest/get";
    	if($scope.isProject){
    		$scope.info.dataCss="box"
    	}
    	$scope.info.allProject=false;
    	$scope.info.failureRecord=false;
    	$scope.info.startDate=null;
    	$scope.info.endDate=null;
    	$scope.page = {size: 15, index: 1};
    	$scope.info.recordList=[];
    	$scope.info.projects=[];
    	$scope.info.projectSelect=[];
	    $scope.getProjects = function () {
	        $http.get(beetle_plan_url + '/project_plan/getPlanList?&token_id=' + getTokenId() + '&isShowOnline=false').success(function (planDatas) {
	        	angular.forEach(planDatas.data, function(planData){
	        		splicingProjectData(planData.plan_id,planData.plan_name)
	        	})
	        });
            $http.get(beetle_plan_url+'/integration/getIntegrationPlanList?token_id='+getTokenId()+'&hasProList=false').success(function(integrationDatas){
            angular.forEach(integrationDatas.data, function(integrationData){
                splicingProjectData(integrationData.integration_id,integrationData.integration_name)
            })
        })
	    };
	    
	    function splicingProjectData(id,name){
	    	var obj={};
	        	obj.id=id;
	        	obj.name=name;
	        $scope.info.projects.push(obj)
	    }
	    $scope.getProjects();



    //打开新页面
      $scope.open=function(i){
        window.open('http://'+i.agent_ip+":"+uTest_port+i.report_path)
      }

    	$scope.getRecords=function(searchUrl){
            var url=beetle_plan_url+"/autotest/get"
            if(searchUrl!=undefined){
                url=url+searchUrl
            }
    		$http.get(url).success(function(data){
                if(data.success){
                    $scope.info.recordList=data.data;
                    $scope.total=$scope.info.recordList.length;
                }else{
                    alert(data.msg)
                }
    			
    		})
    	}
    	$scope.search=function(){
            var ids="";
            var url="";
            if(!$scope.info.failureRecord){
                $scope.info.status="ALL";
            }else{
                $scope.info.status="FAIL";
            }
            url="?status="+$scope.info.status

            if(!$scope.isProject){
                ids=$scope.projectId
                url=url+"&plans="+ids;
            }
            if($scope.info.projectSelect.length!=0 && !$scope.info.allProject){
                angular.forEach($scope.info.projectSelect, function(projec){
                    ids+=projec.id+","
                })
                ids=ids.substring(0, ids.length - 1)
                 url=url+"&plans="+ids;
            }
            if($scope.info.startDate!=null){
                var sDate=$filter('date')($scope.info.startDate, "yyyy-MM-dd 00:00:00")
                url=url+"&start_date="+sDate;
            }
            if($scope.info.endDate!=null){
                var eDate=$filter('date')($scope.info.endDate, "yyyy-MM-dd 23:59:59")
                url=url+"&end_date="+eDate;
            }
            $scope.getRecords(url);
    	}
        $scope.search()

     }], 
    link: function (scope, element, attrs, controller) {} 
  } 
});  
